using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using BTV.Base;
using System.Xml;

namespace BTV.Bleb
{
    public class BlebChannelBuilder
    {
        private List<BTVChannel> _channelsList = new List<BTVChannel>();

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
        public List<BTVChannel> ChannelsList
        {
            get { return _channelsList; }
        }
        private bool _all = true;

        public bool All
        {
            get { return _all; }
            set { _all = value; }
        }
        private StringCollection _allChannels;

        public StringCollection AllChannels
        {
            get { return _allChannels; }
        }
        private StringCollection _channels;

        public StringCollection Channels
        {
            get { return _channels; }
        }

        public BlebChannelBuilder(bool all, StringCollection allChannels, StringCollection channels)
        {
            this._all = all;
            _allChannels = allChannels;
            _channels = channels;
            GenerateDisplayNames();
        }

        private Dictionary<string, string> _displayNames;

        private void GenerateDisplayNames()
        {
            _displayNames = new Dictionary<string, string>();
            XmlDocument d = new XmlDocument();
            d.LoadXml(BTV.Base.Properties.Resources.BlebDisplayNames);
            XmlNodeList channels = d.SelectNodes(".//channel");
            foreach (XmlNode channel in channels)
            {
                XmlNode idNode = channel.ChildNodes[0];
                XmlNode dnNode = channel.ChildNodes[1];
                _displayNames.Add(idNode.InnerText, dnNode.InnerText);
            }            
        }

        public void BuildChannelList()
        {
            if ( _all )
            {
                foreach (string cname in _allChannels)
                {
                    BTVChannel c = new BTVChannel();
                    c.Id = cname;
                    c.Name = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(cname);
                    c.IsTVAny = false;
                    string dn;
                    if(_displayNames.TryGetValue(cname, out dn))
                        c.DisplayName = dn;
                    _channelsList.Add(c);
                }
            }
            else
            {
                if (_channels != null)
                {
                    foreach (string cname in _channels)
                    {
                        BTVChannel c = new BTVChannel();
                        c.Id = cname;
                        c.Name = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(cname);
                        c.IsTVAny = false;
                        string dn;
                        if (_displayNames.TryGetValue(cname, out dn))
                            c.DisplayName = dn;
                        _channelsList.Add(c);
                    }
                }
            }
        }
    }
}
